package com.xudow.app.ui.user;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.order.AgencyOrderDetail;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.CourseDetailActivity;
import com.xudow.app.ui.OrderDetailActivity;
import com.xudow.app.ui.OrderPreviewActivity;
import com.xudow.app.ui.adapter.OrderListAdapter;
import com.xudow.app.ui.adapter.user.OrderAdapter;
import com.xudow.app.ui.support.RecyclerViewScrollLocationListener;
import com.xudow.app.ui.support.XLinearLayoutManager;
import com.xudow.app.ui.task.ClazzMistakeRemoveTask;
import com.xudow.app.ui.task.OrderDeleteTask;
import com.xudow.app.ui.task.OrderDetailTask;
import com.xudow.app.ui.task.OrderListTask;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 我的订单ListFragment
 *
 * @author lukecheng
 */
public class OrderListFragment extends Fragment implements ExpandableListView.OnChildClickListener {
    private List<AgencyOrderDetail> orders;
    private OrderListAdapter orderAdapter;

    private ExpandableListView exlist;
    private ProgressBar progressBar;
    private DataUpdateListener dataUpdateListener;
    private RelativeLayout emptyview;
    private TextView empty_inf_tv;
    public void setDataUpdateListener(DataUpdateListener dataUpdateListener){
        this.dataUpdateListener=dataUpdateListener;

    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        AgencyOrderDetail order = orders.get(groupPosition);

        //课程有个status的状态，如果是 COURSE_STATUS_INVALID = 0 则表示无效了
        //当前没有购物车，一个订单不存在多个商品，取第一个即可t
        if (0 == order.getItems().get(childPosition).getCourse().getStatus()) {
            String msg = getString(R.string.course_invalid);
            Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
            return true;
        }

        //已支付订单点击后跳转至订单详情，未支付点击后跳去“订单详情”页面
        if (order.getStatus() == OrdersActivity.ORDER_STATUS_PAID) {
            Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
            intent.putExtra("orderId", "" +order.getId());
            startActivity(intent);
        } else if (order.getStatus() == OrdersActivity.ORDER_STATUS_UNPAY) {
            Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
            intent.putExtra("orderId", "" + order.getId());
            startActivity(intent);
        }
        return true;
    }


    static OrderListFragment newInstance(int index) {
        OrderListFragment f = new OrderListFragment();
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_orderlist, container, false);

        progressBar = (ProgressBar) v.findViewById(R.id.progressbar);

        exlist = (ExpandableListView) v.findViewById(R.id.order_list);
        emptyview=(RelativeLayout)v.findViewById(R.id.empty_view);
        empty_inf_tv=(TextView)v.findViewById(R.id.emty_inf_tv);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        orders = new ArrayList<AgencyOrderDetail>();
        orderAdapter = new OrderListAdapter(getActivity(),orders);
        exlist.setAdapter(orderAdapter);


        orderAdapter.setDataUpdateListener(dataUpdateListener);
        //exlist.setOnChildClickListener(this);
        exlist.setChildDivider(null);//child分割线
        exlist.setDivider(null);
        exlist.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
    }

    public void setOrders(List<AgencyOrderDetail> orders) {
        emptyview.setVisibility(View.GONE);
        if(this.orders.size()>0)
            this.orders.clear();

            this.orders.addAll(orders);
            progressBar.setVisibility(View.GONE);
            exlist.setVisibility(View.VISIBLE);
            orderAdapter.notifyDataSetChanged();
            for (int i = 0; i < orders.size(); i++) {
                exlist.expandGroup(i);
            }
            exlist.setGroupIndicator(null);

        if(orders.size()==0){//没有订单显示 empty View
            emptyview.setVisibility(View.VISIBLE);
            exlist.setVisibility(View.GONE);
            empty_inf_tv.setText("亲,没有要显示的订单");
        }
    }


}